package de.tum.ibis.wsc.aufgabe23;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LandKundenServlet extends HttpServlet{
		public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
			String code = request.getParameter("code");
			File xmlFile = new File(getServletContext().getRealPath("/WEB-INF/" + code + ".xml"));
			if(!xmlFile.exists()){
				xmlFile.createNewFile();
				Writer out = new BufferedWriter(new FileWriter(xmlFile));
				out.write("<?xml version=\"1.0\" encoding= \"ISO-8859-1\"?>\n");
				out.write("<Kunden>\n");
				out.write("<Kunde nachname=\"" + UUID.randomUUID() + "\" vorname=\"" + UUID.randomUUID() + "\" geschlecht=\"" + UUID.randomUUID() + "\" geburtsjahr=\"" + UUID.randomUUID() + "\">\n");
				out.write("<KundenNr>" + UUID.randomUUID() + "</KundenNr>\n");
				out.write("<Anschrift><PLZ>"+ UUID.randomUUID()  +"</PLZ><Ort>"+ UUID.randomUUID()  +"</Ort></Anschrift>\n");
				out.write("<Hobbies>"+ UUID.randomUUID()  +"</Hobbies>\n");
				out.write("</Kunde>\n");
				out.write("<Kunde nachname=\"" + UUID.randomUUID() + "\" vorname=\"" + UUID.randomUUID() + "\" geschlecht=\"" + UUID.randomUUID() + "\" geburtsjahr=\"" + UUID.randomUUID() + "\">\n");
				out.write("<KundenNr>" + UUID.randomUUID() + "</KundenNr>\n");
				out.write("<Anschrift><PLZ>"+ UUID.randomUUID()  +"</PLZ><Ort>"+ UUID.randomUUID()  +"</Ort></Anschrift>\n");
				out.write("<Hobbies>"+ UUID.randomUUID()  +"</Hobbies>\n");
				out.write("</Kunde>\n");
				out.write("</Kunden>\n");		
				out.close();
			}		
			request.setAttribute("landcode",code);
			request.setAttribute("xmlfile", "/WEB-INF/" + code + ".xml");
			request.setAttribute("xsltfile", "/WEB-INF/customers.xslt");
			getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
		}		
}
